package com.ruoyi.system.controller;

import java.io.InputStream;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GameInfo;
import com.ruoyi.system.service.IGameInfoService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 游戏 信息操作处理
 *
 * @author ruoyi
 * @date 2019-02-05
 */
@Controller
@RequestMapping("/system/gameInfo")
public class GameInfoController extends BaseController {
    private String prefix = "system/gameInfo";
    private static Logger log = LoggerFactory.getLogger(BaseController.class);
    @Autowired
    private IGameInfoService gameInfoService;

    @RequiresPermissions("system:gameInfo:view")
    @GetMapping()
    public String gameInfo() {
        return prefix + "/gameInfo";
    }

    /**
     * 查询游戏列表
     */
    @RequiresPermissions("system:gameInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GameInfo gameInfo) {
        startPage();
        List<GameInfo> list = gameInfoService.selectGameInfoList(gameInfo);
        return getDataTable(list);
    }


    /**
     * 导出游戏列表
     */
    @RequiresPermissions("system:gameInfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GameInfo gameInfo) {
        List<GameInfo> list = gameInfoService.selectGameInfoList(gameInfo);
        ExcelUtil<GameInfo> util = new ExcelUtil<GameInfo>(GameInfo.class);
        return util.exportExcel(list, "gameInfo");
    }

    /**
     * 新增游戏
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存游戏
     */
    @RequiresPermissions("system:gameInfo:add")
    @Log(title = "游戏", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GameInfo gameInfo) {
        return toAjax(gameInfoService.insertGameInfo(gameInfo));
    }

    /**
     * 修改游戏
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        GameInfo gameInfo = gameInfoService.selectGameInfoById(id);
        mmap.put("gameInfo", gameInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存游戏
     */
    @RequiresPermissions("system:gameInfo:edit")
    @Log(title = "游戏", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GameInfo gameInfo) {
        return toAjax(gameInfoService.updateGameInfo(gameInfo));
    }

    /**
     * 删除游戏
     */
    @RequiresPermissions("system:gameInfo:remove")
    @Log(title = "游戏", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(gameInfoService.deleteGameInfoByIds(ids));
    }
    @ResponseBody
    @PostMapping(value = "/import")
    public AjaxResult importGameInfo(MultipartFile file) {
        try {
            InputStream is = file.getInputStream();
            XSSFWorkbook wb = new XSSFWorkbook(is);
            XSSFSheet sheet = wb.getSheetAt(0);
            //取得最后一行的行号
            int rowNum = sheet.getLastRowNum() + 1;

            int all = 0;
            // 跳过第一行
            GameInfo gameInfo = null;
            for (int i = 1; i < rowNum; i++) {
                XSSFRow row = sheet.getRow(i);

                String name = getCellValue(row.getCell(0));
                String context = getCellValue(row.getCell(1));
                String detail = getCellValue(row.getCell(2));
                String logo = getCellValue(row.getCell(3));
                String imgs = getCellValue(row.getCell(4));
                String apkAddress = getCellValue(row.getCell(5));
                String fromType = getCellValue(row.getCell(6));

                gameInfo = new GameInfo();
                gameInfo.setName(name);
                gameInfo.setContext(context);
                gameInfo.setText(detail);
                gameInfo.setLogo(logo);
                gameInfo.setImgs(imgs);
                gameInfo.setApkAddress(apkAddress);
                gameInfo.setFromType(fromType);
                gameInfoService.insertGameInfo(gameInfo);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return toAjax(false);
        }
        return success();
    }

    private String getCellValue(XSSFCell cell) {
        // String cellValue = "";
        // if (null != cell) {
        //     // 判断excel单元格内容的格式，并对其进行转换，以便插入数据库
        //     switch (cell.getCellType()) {
        //         case 0:
        //             cellValue = String.valueOf((int) cell.getNumericCellValue());
        //             break;
        //         case 1:
        //             cellValue = cell.getStringCellValue();
        //             break;
        //         case 2:
        //             cellValue = cell.getNumericCellValue() + "";
        //             // cellValue = String.valueOf(cell.getDateCellValue());
        //             break;
        //         case 3:
        //             cellValue = "";
        //             break;
        //         case 4:
        //             cellValue = String.valueOf(cell.getBooleanCellValue());
        //             break;
        //         case 5:
        //             cellValue = String.valueOf(cell.getErrorCellValue());
        //             break;
        //         default:
        //             cellValue = "";
        //     }
        // }
        return cell.getStringCellValue().trim();
    }
}